﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using JXC.Core.Models;
using JXC.EFCore;

namespace JXC.WebAPI.Controllers
{
    /// <summary>
    /// 采购入库明细
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CaigouStorageDetailsController : ControllerBase
    {
        private readonly AppDbContext _context;

        public CaigouStorageDetailsController(AppDbContext context)
        {
            _context = context;
        }

        // GET: api/CaigouStorageDetails
        [HttpGet]
        public async Task<ActionResult<IEnumerable<CaigouStorageDetails>>> GetCaigouStorageDetails()
        {
            return await _context.CaigouStorageDetails.ToListAsync();
        }

        // GET: api/CaigouStorageDetails/5
        [HttpGet("{id}")]
        public async Task<ActionResult<CaigouStorageDetails>> GetCaigouStorageDetails(string id)
        {
            var caigouStorageDetails = await _context.CaigouStorageDetails.FindAsync(id);

            if (caigouStorageDetails == null)
            {
                return NotFound();
            }

            return caigouStorageDetails;
        }

        // PUT: api/CaigouStorageDetails/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutCaigouStorageDetails(string id, CaigouStorageDetails caigouStorageDetails)
        {
            if (id != caigouStorageDetails.Id)
            {
                return BadRequest();
            }

            _context.Entry(caigouStorageDetails).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CaigouStorageDetailsExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/CaigouStorageDetails
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<CaigouStorageDetails>> PostCaigouStorageDetails(CaigouStorageDetails caigouStorageDetails)
        {
            _context.CaigouStorageDetails.Add(caigouStorageDetails);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetCaigouStorageDetails", new { id = caigouStorageDetails.Id }, caigouStorageDetails);
        }

        // DELETE: api/CaigouStorageDetails/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteCaigouStorageDetails(string id)
        {
            var caigouStorageDetails = await _context.CaigouStorageDetails.FindAsync(id);
            if (caigouStorageDetails == null)
            {
                return NotFound();
            }

            _context.CaigouStorageDetails.Remove(caigouStorageDetails);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool CaigouStorageDetailsExists(string id)
        {
            return _context.CaigouStorageDetails.Any(e => e.Id == id);
        }
    }
}
